import ScreenPositionUtil from "./ScreenPositionUtil";
import GameConfig from "./GameConfig";
import DrawUtil from './DrawUtil'

/**
 * 网格区域
 */
export default class ContentGridRoot extends Laya.Sprite {
    constructor(inParent) {
        super();
        this.parentMain = inParent;
		this.bgBigImgView = new Laya.Sprite;
		this.addChild(this.bgBigImgView);

        // 外网格线group
        this.dataOutsideBoirders = [];
        this.viewOutsideGridBorderRoot = new Laya.Sprite;
        this.addChild(this.viewOutsideGridBorderRoot);
        // 内网格线group
        this.dataInsideBoirders = [];
        this.viewInsideGridBorderRoot = new Laya.Sprite;
        this.addChild(this.viewInsideGridBorderRoot);

        let posX = ScreenPositionUtil.getPostionInStage(GameConfig.marginLeftRightRatio, "x");
        let posY = ScreenPositionUtil.getPostionInStage(GameConfig.marginTopRatio, "y");
        GameConfig.GridWidth = (Laya.stage.width - 2 * posX) / GameConfig.GridSumH;
        GameConfig.RootStartX = posX;
        GameConfig.RootStartY = posY;

        GameConfig.RootWidth = (Laya.stage.width - 2 * posX);
        GameConfig.RootHeight = GameConfig.GridWidth * GameConfig.GridSumV;
        GameConfig.elementPadding = GameConfig.GridWidth * GameConfig.elementPaddingRatio;

        this.pos(GameConfig.RootStartX, GameConfig.RootStartY);

        this.bgBigImgView.size(GameConfig.RootWidth ,GameConfig.RootHeight);
		this.bgBigImgView.loadImage(GameConfig.gameData.backgroundImg);

        this.initOutsideGrid();
        this.initInsideGrid();
    }

    initOutsideGrid() {
        // top
        this.viewOutsideGridBorderRoot.addChild(DrawUtil.addBorderLine(0, 0, 0, GameConfig.RootWidth, GameConfig.OutsideGridBorderWidth,"DCDCDC11"));
        this.dataOutsideBoirders.push({
            x: 0, y: 0, w: GameConfig.RootWidth, h: GameConfig.OutsideGridBorderWidth
        });
        // bottom
        this.viewOutsideGridBorderRoot.addChild(DrawUtil.addBorderLine(1, 0, GameConfig.RootHeight, GameConfig.RootWidth, GameConfig.OutsideGridBorderWidth,"DCDCDC11"));
        this.dataOutsideBoirders.push({
            x: 0, y: GameConfig.RootHeight, w: GameConfig.RootWidth, h: GameConfig.OutsideGridBorderWidth
        });
        // left
        this.viewOutsideGridBorderRoot.addChild(DrawUtil.addBorderLine(2, 1, 1, GameConfig.OutsideGridBorderWidth, GameConfig.RootHeight,"DCDCDC11"));
        this.dataOutsideBoirders.push({
            x: 1, y: 1, w: GameConfig.OutsideGridBorderWidth, h: GameConfig.RootHeight
        });
        // right
        this.viewOutsideGridBorderRoot.addChild(DrawUtil.addBorderLine(3, GameConfig.RootWidth, 0, GameConfig.OutsideGridBorderWidth, GameConfig.RootHeight,"DCDCDC11"));
        this.dataOutsideBoirders.push({
            x: GameConfig.RootWidth, y: 0, w: GameConfig.OutsideGridBorderWidth, h: GameConfig.RootHeight
        });
    }

    initInsideGrid() {
        for (let i = 0; i < GameConfig.GridSumH - 1; i++) {
            let x = GameConfig.GridWidth * (i + 1);
            let y = 0;
            let w = GameConfig.InsideGridBorderWidth;
            let h = GameConfig.RootHeight;

            this.viewInsideGridBorderRoot.addChild(
                DrawUtil.addBorderLine(i, x, y, w, h, null, "#DCDCDC11")
            );
            this.dataInsideBoirders.push({ x, y, w, h, parent: null, strokeColor: "#DCDCDC11" })
        }

        for (let j = 0; j < GameConfig.GridSumV - 1; j++) {
            let x = 0;
            let y = GameConfig.GridWidth * (j + 1);
            let h = GameConfig.InsideGridBorderWidth;
            let w = GameConfig.RootWidth;

            this.viewInsideGridBorderRoot.addChild(
                DrawUtil.addBorderLine(j, x, y, w, h, null, "#DCDCDC11")
            );
            this.dataInsideBoirders.push({ x, y, w, h, parent: null, strokeColor: "#DCDCDC11" })
        }
    }

    updateGridVisible(e) {
        this.viewOutsideGridBorderRoot.visible = e;
        this.viewInsideGridBorderRoot.visible = e;
    }
}